package com.example.springbott.wx_controller;

import com.example.springbott.bean.MarketCoupon;
import com.example.springbott.bean.MarketCouponUser;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.BaseRespVo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.zeng.BaseRespVoInDelete;
import com.example.springbott.wx_service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: 小朱
 * @Description:
 * @date: 11:34 2022/11/22
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    //-----------------------小朱-----------------------
    //status=0&page=1&limit=10
    @RequestMapping("mylist")
    //()括号里面的表示的是接收的请求参数的变量
    public BaseRespVo wxCouponMylist(Short status, Integer page, Integer limit) {
        CommonData commonData = wxCouponService.wxCouponMylist(status, page, limit);
        return BaseRespVo.ok(commonData);
    }

    //page=1&limit=10
    @RequestMapping("list")
    public BaseRespVo wxCouponList(MarketCoupon marketCoupon, BasePageInfo basePageInfo) {
        CommonData commonData = wxCouponService.wxCouponList(marketCoupon, basePageInfo);
        return BaseRespVo.ok(commonData);
    }
    //couponId: 2
    @RequestMapping("receive")
    //用keyValue值，map来接收单个请求参数
    public BaseRespVo wxCouponReceive(@RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");
        MarketCouponUser marketCouponUser = wxCouponService.wxCouponReceive(couponId);
        return BaseRespVo.ok(marketCouponUser);
    }
    @RequestMapping("exchange")
    public  BaseRespVoInDelete wxCouponExchange(@RequestBody Map map){
        String code = (String) map.get("code");
        Integer result = wxCouponService.wxCouponExchange(code);
        if (result == 0) {
            return BaseRespVoInDelete.invalidData("兑换失败");
        }
        return BaseRespVoInDelete.ok();

    }
    //cartId: 0    grouponRulesId: 0
    @RequestMapping("selectlist")
    public com.example.springbott.bean.BaseRespVo wxCouponSelectList(Integer cartId){
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            return com.example.springbott.bean.BaseRespVo.fail("请登录", 501);
        }
        CommonData commonData = wxCouponService.wxCouponSelectlist(cartId);
        return com.example.springbott.bean.BaseRespVo.ok(commonData);
    }
}